<template>
  <div id="translate">
    <!--搜索-->
    <div class="search-content">
      <div>
        <el-input v-model="code" placeholder="请输入语言代码查询" prefix-icon="el-icon-search" clearable @change="inquiryList" />
        <el-input v-model="zhCnName" placeholder="请输入语言名称查询" prefix-icon="el-icon-search" clearable @change="inquiryList" />
        <el-select v-model="enable" clearable placeholder="请选择是否启用翻译"  @change="inquiryList" >
          <el-option
            v-for="item in enableList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
        <el-button type="primary" icon="el-icon-search" @click="inquiryList">查询</el-button>
      </div>
      <div class="search-apply">
        <el-button type="warning" icon="el-icon-edit" @click="addUpload">新增</el-button>
      </div>
    </div>
    <!--操作列表-->
    <div class="table">
      <el-table fit :data="tableData" style="width: 100%">
        <el-table-column label="语言代码" prop="code"  align="center" />
        <el-table-column label="语言名称" prop="zhCnName"  align="center" />
        <el-table-column label="是否启用翻译" prop="enable"  align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.enable">启用</span>
            <span v-else>不启用</span>
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createDate" width="170"  align="center" />
        <el-table-column label="操作"  width="300" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="warning" @click="dealwithEdit(scope.$index, scope.row)">修改</el-button>
            <el-button size="mini" type="danger" @click="dealwithDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页-->
      <div class="pagination">
        <el-pagination
          :current-page.sync="currentPage"
          :page-sizes="[10,20,30,40,50]"
          layout="total,sizes, prev, pager, next"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <!--新增蒙层-->
    <el-dialog id="add-dialog" title="新增" :visible.sync="addDialogVisible">
      <el-form label-position="right" label-width="30%"  :model="add"  :rules="addRules" ref="addRuleForm">
        <el-form-item label="语言代码：" prop="code">
          <el-input v-model="add.code" placeholder="请输入语言代码" clearable />
        </el-form-item>
        <el-form-item label="简体名称：" prop="zhCnName">
          <el-input v-model="add.zhCnName" placeholder="请输入简体语言名称" clearable />
        </el-form-item>
        <el-form-item label="繁体名称：" prop="zhTwName">
          <el-input v-model="add.zhTwName" placeholder="请输入繁体语言名称" clearable />
        </el-form-item>
        <el-form-item label="英文名称：" prop="enUsName">
          <el-input v-model="add.enUsName" placeholder="请输入英文语言名称" clearable />
        </el-form-item>
        <el-form-item label="日文名称：" prop="jaName">
          <el-input v-model="add.jaName" placeholder="请输入日文语言名称" clearable />
        </el-form-item>
        <el-form-item label="是否翻译：">
          <el-switch v-model="add.enable" inactive-text="不启用" active-text="启用"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button id="add-btn" type="primary" @click="addSubmit('addRuleForm')">新 增</el-button>
      </span>
    </el-dialog>
    <!--修改蒙层-->
    <el-dialog id="edit-dialog" title="修改" :visible.sync="editDialogVisible">
      <el-form label-position="right" label-width="30%"  :model="edit"  :rules="editRules" ref="editRuleForm">
        <el-form-item label="简体名称：" prop="zhCnName">
          <el-input v-model="edit.zhCnName" placeholder="请输入简体语言名称" clearable />
        </el-form-item>
        <el-form-item label="繁体名称：" prop="zhTwName">
          <el-input v-model="edit.zhTwName" placeholder="请输入繁体语言名称" clearable />
        </el-form-item>
        <el-form-item label="英文名称：" prop="enUsName">
          <el-input v-model="edit.enUsName" placeholder="请输入英文语言名称" clearable />
        </el-form-item>
        <el-form-item label="日文名称：" prop="jaName">
          <el-input v-model="edit.jaName" placeholder="请输入日文语言名称" clearable />
        </el-form-item>
        <el-form-item label="是否翻译：">
          <el-switch v-model="edit.enable" inactive-text="不启用" active-text="启用"/>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button id="edit-btn" type="primary" @click="editSubmit('editRuleForm')">修 改</el-button>
      </span>
    </el-dialog>
    <!--删除-->
    <el-dialog :visible.sync="delDialogVisible" width="300px" center id="del-dialog" top="20%">
      <span class="del-tips">您确定要删除吗？</span>
      <span slot="footer" class="dialog-footer">
          <el-button @click="delDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="delKeys">确 定</el-button>
        </span>
    </el-dialog>
  </div>
</template>

<script>
  import {Message} from 'element-ui';
  import request from "../../utils/request";
  import axios from 'axios'
    export default {
        name: "TranslateManagment",
      data(){
        var checkPublic = (rule, value, callback) => {
          if (!value) {
            return callback(new Error('内容不能为空'));
          }else {
            callback();
          }
        };
        return{
          // 查询
          code:'',
          zhCnName:'',
          enable:null,
          enableList:[
            {
              value:true,
              label:"启用"
            },
            {
              value:false,
              label:"不启用"
            }
          ],
          // 列表数据
          tableData:[],
          //新增
          addDialogVisible:false,
          add:{
            code:'',
            zhCnName:'',
            zhTwName:'',
            enUsName:'',
            jaName:'',
            enable:true
          },
          addRules:{
            code:[{validator: checkPublic, trigger: 'blur' }],
            zhCnName:[{validator: checkPublic, trigger: 'blur' }],
            zhTwName:[{validator: checkPublic, trigger: 'blur' }],
            enUsName:[{validator: checkPublic, trigger: 'blur' }],
            jaName:[{validator: checkPublic, trigger: 'blur' }]
          },
          //修改
          editDialogVisible:false,
          edit:{
            id:null,
            zhCnName:'',
            zhTwName:'',
            enUsName:'',
            jaName:'',
            enable:true
          },
          editRules:{
            zhCnName:[{validator: checkPublic, trigger: 'blur' }],
            zhTwName:[{validator: checkPublic, trigger: 'blur' }],
            enUsName:[{validator: checkPublic, trigger: 'blur' }],
            jaName:[{validator: checkPublic, trigger: 'blur' }]
          },
          // 删除
          delId:null,
          delDialogVisible:false,
          //分页
          currentPage: 1,
          pageSize:10,
          total: 0
        }
      },
      mounted() {
        this.inquiryList();
      },
      methods:{
        isNotNull(value){
          return value !== null && value !== undefined && value !== "";
        },
        //查询列表
        inquiryList(){
          let that=this;
          that.tableData=[];
          let data={
            pageNumber:that.currentPage,
            pageSize:that.pageSize,
            filters:[]
          };
          if(this.isNotNull(that.code)){  //语言代码
            data.filters.push({
              property:"code",
              value:that.code,
              operator: "like"
            })
          }
          if(this.isNotNull(that.zhCnName)){  //中文语言名称
            data.filters.push({
              property:"zh_cn_name",
              value:that.zhCnName,
              operator: "like"
            })
          }
          if(this.isNotNull(that.enable)){ //是否启用翻译
            data.filters.push({
              property:"enable",
              value:that.enable,
              operator: "eq"
            })
          }
          request({
            method: 'post',
            url: 'admin/translate/list',
            headers:{
              'Content-Type': 'application/json;charset=UTF-8'
            },
            data:data
          }).then(function (res) {
            if(res.code===200){
              that.total=res.data.total;
              let arr=res.data.records;
              if(arr.length===0){
                that.tableData=[];
              }else{
                that.tableData=arr
              }
            }
          }).catch(function (error) {
            console.log(error);
          });
        },
        //新增
        addUpload(){
          this.addDialogVisible=true;
        },
        addSubmit(formName){
          this.$refs[formName].validate((valid) => {
            if (valid) {
              let that=this;
              request({
                method: 'post',
                url: 'admin/translate/save',
                data:that.add
              }).then(function (res) {
                if(res.code===200){
                  that.addDialogVisible = false;
                  Message.success("新增成功");
                  that.$refs['addRuleForm'].resetFields();
                  that.inquiryList();
                }
              }).catch(function (error) {
                console.log(error);
              });
            }else {
              return false;
            }
          });
        },
        //修改
        dealwithEdit(index, row) {
          this.editDialogVisible = true;
          this.edit = row;
        },
        editSubmit(formName){
          this.$refs[formName].validate((valid) => {
            if (valid) {
              let that=this;
              delete that.edit.createDate;
              delete that.edit.modifyDate;
              request({
                method: 'post',
                url: 'admin/translate/update',
                data:that.edit
              }).then(function (res) {
                if(res.code===200){
                  that.editDialogVisible = false;
                  Message.success("修改成功");
                  that.inquiryList();
                }
              }).catch(function (error) {
                console.log(error);
              });
            }else {
              return false;
            }
          });
        },
        //删除
        dealwithDelete(index, row){
          this.delDialogVisible=true;
          this.delId=row.id;
        },
        delKeys(){
          let that=this;
          that.delDialogVisible=false;
          request({
            method: 'post',
            url: 'admin/translate/delete/'+that.delId,
          }).then(function (res) {
            if(res.code===200){
              Message.success("删除成功");
              that.delId=null;
              that.inquiryList();
            }
          }).catch(function (error) {
            console.log(error);
          });
        },
        // 改变每页显示的数量
        handleSizeChange(val) {
          this.pageSize=val;
          this.inquiryList();
        },
        //改变当前页
        handleCurrentChange(val) {
          this.currentPage=val;
          this.inquiryList();
        }
      }
    }
</script>

<style src="../../assets/css/translateManagement/translateList.css">

</style>
